<html>
<head><meta charset="utf-8"><title>Vec::remove_range · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html">Vec::remove_range</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206745799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206745799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206745799">(Aug 12 2020 at 20:18)</a>:</h4>
<p>Hello!<br>
I needed a way to remove a range from a <code>Vec&lt;u8&gt;</code> and I ended up understanding that I should use <code>Vec::drain</code>, however that does seem overly complicated for basically just a memmove. Moreover, whats the best way to consume the iterator returned by drain? I found <code>https://github.com/rust-lang/rust/pull/48945</code> which seem to imply I should call <code>.drop()</code> on the returned iterator? Even if my elements are <code>T: Copy</code>?</p>



<a name="206746154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746154">(Aug 12 2020 at 20:21)</a>:</h4>
<p>I think you can literally do <code>the_vec.drain(start..end);</code> the returned iterator is dropped because there is no owner</p>



<a name="206746180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746180">(Aug 12 2020 at 20:21)</a>:</h4>
<p>I guess I could also implement remove_range using copy_within + unsafe call to set_len. It's a bit unfortunate that I have to rely of unsafe code for what seems to be a safe operation</p>



<a name="206746197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746197">(Aug 12 2020 at 20:21)</a>:</h4>
<p>oh</p>



<a name="206746330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746330">(Aug 12 2020 at 20:22)</a>:</h4>
<p>Test that out, at least.</p>



<a name="206746334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746334">(Aug 12 2020 at 20:22)</a>:</h4>
<p>I see <code>Note 1: The element range is removed even if the iterator is only partially consumed or not consumed at all.</code></p>



<a name="206746370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746370">(Aug 12 2020 at 20:22)</a>:</h4>
<p>For more complicated cases, there's <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html#method.drain_filter">https://doc.rust-lang.org/std/vec/struct.Vec.html#method.drain_filter</a></p>



<a name="206746450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746450">(Aug 12 2020 at 20:23)</a>:</h4>
<p>I don't understand how <code>Note 2: It is unspecified how many elements are removed from the vector if the Drain value is leaked.</code> relates to <code>Note 1</code></p>



<a name="206746512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746512">(Aug 12 2020 at 20:23)</a>:</h4>
<p>Either all elements are removed not matter what you do with the iterator, or not</p>



<a name="206746617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746617">(Aug 12 2020 at 20:24)</a>:</h4>
<p>It might be an english issue, but I feel like note 2 kind of contradicts note 1?</p>



<a name="206746937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746937">(Aug 12 2020 at 20:26)</a>:</h4>
<p>Note 2 says that if you, for example, do <code>mem::forget(vec.drain(start..end))</code> you might end up with an empty vector</p>



<a name="206746965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206746965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206746965">(Aug 12 2020 at 20:27)</a>:</h4>
<p>(or a completely full one, with no elements removed, or really anything in between)</p>



<a name="206747100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206747100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206747100">(Aug 12 2020 at 20:28)</a>:</h4>
<p>Ok</p>



<a name="206747161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206747161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206747161">(Aug 12 2020 at 20:28)</a>:</h4>
<p>thank you!</p>



<a name="206747211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206747211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206747211">(Aug 12 2020 at 20:29)</a>:</h4>
<p>I think everything makes sense now :) Maybe the doc could use some more examples though</p>



<a name="206747296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206747296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206747296">(Aug 12 2020 at 20:30)</a>:</h4>
<p>I'll see what I can do if I ever find the time to improve it</p>



<a name="206760557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206760557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206760557">(Aug 12 2020 at 22:32)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> I remember modifying exactly those docs recently, I don’t think the changes have landed in stable yet</p>



<a name="206760787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206760787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206760787">(Aug 12 2020 at 22:35)</a>:</h4>
<p>Yep: <a href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html#method.drain">https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html#method.drain</a></p>



<a name="206760924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206760924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206760924">(Aug 12 2020 at 22:37)</a>:</h4>
<p>oh great, its clearer now!</p>



<a name="206760930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206760930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206760930">(Aug 12 2020 at 22:37)</a>:</h4>
<p>thank you!</p>



<a name="206761030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Aremove_range/near/206761030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Aremove_range.html#206761030">(Aug 12 2020 at 22:38)</a>:</h4>
<p>I have nothing to improve anymore :P</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>